package com.reyco1.gadget
{
	/*
	imports
	*/
	import com.reyco1.extension.DispatchExtension;
	import com.reyco1.events.GridEvent;
	import flash.display.DisplayObjectContainer;
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	
	/*
	class
	*/
	public class Grid extends DispatchExtension
	{
		/*
		variables
		*/
		private var container:DisplayObjectContainer;
		private var gridWidth:Number;
		private var gridHeight:Number;
		private var gridItem:*;
		private var columns:Number;
		private var rows:Number;
		private var xSpace:Number;
		private var ySpace:Number;
		private var index:Number;
		private var dictionary:Dictionary;
		/*
		class
		*/
		public function Grid():void
		{
		}
		
		/*
		function
		*/
		
		public function setup($parameters:Object):void
		{
			this.container = $parameters.container;
			this.gridItem = $parameters.item;
			this.columns = $parameters.columns;
			this.rows = $parameters.rows;
			this.xSpace = $parameters.xSpace || 5;
			this.ySpace = $parameters.ySpace || 5; 
			this.index = 0;
			this.dictionary = new Dictionary(true);
		}
		
		public function create():void
		{
			for (var b:Number = 0; b < this.rows; b++)
			{
				for (var a:Number = 0; a < this.columns; a++)
				{
					var item:* = new this.gridItem;
					this.container.addChild(item);
					
					this.gridWidth = (item.width * this.columns) + (this.xSpace * (this.columns - 1));
					this.gridHeight = (item.height * this.rows) + (this.ySpace * (this.rows - 1));
					
					item.x = a * (Math.floor(this.gridWidth / this.columns));
					item.y = b * (Math.floor(this.gridHeight / this.rows));
					
					this.dictionary[this.index] = item;
					this.index++;
				}
			}
			this.dispatchEvent(new GridEvent(GridEvent.CREATE, {}));
		}
		
		public function get width():Number
		{
			return this.container.width;
		}
		
		public function get height():Number
		{
			return this.container.height;
		}
		
		public function getItem($index:Number):*
		{
			return this.dictionary[$index];
		}
	}
}